package com.ikaros.dao;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author: 胡国军
 * @Date 2018-01-28 10:26
 */
public class 坐标转换 {

    @Test
    public void paiduAPI() {
        try {
            Map paramsMap = new LinkedHashMap<String, String>();
            paramsMap.put("ak", "vInhpg2WvEB9AOA40GX1kWdMwmOP6tD9");
            paramsMap.put("coords", "113.977273,22.557837");
            paramsMap.put("from","3");
            // 调用下面的toQueryString方法，对LinkedHashMap内所有value作utf8编码，拼接返回结果address=%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6&output=json&ak=yourak
            String paramsStr = this.toQueryString(paramsMap);

            // 对paramsStr前面拼接上/geocoder/v2/?，后面直接拼接yoursk得到/geocoder/v2/?address=%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6&output=json&ak=yourakyoursk
            String wholeStr = new String("/geoconv/v1/?" + paramsStr);

            // 对上面wholeStr再作utf8编码
            String location = this.getLocation(wholeStr);
            JSONObject data = (JSONObject) JSONObject.parse(location);
            JSONArray resultArray = (JSONArray) data.get("result");
            JSONObject result = (JSONObject)resultArray.get(0);
            String lng = result.getString("x");
            String lat = result.getString("y");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String toQueryString(Map<?, ?> data)
            throws UnsupportedEncodingException {
        StringBuffer queryString = new StringBuffer();
        for (Map.Entry<?, ?> pair : data.entrySet()) {
            queryString.append(pair.getKey() + "=");
            queryString.append(URLEncoder.encode((String) pair.getValue(),
                    "UTF-8") + "&");
        }
        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }
        return queryString.toString();
    }

    public String getLocation(String url) {

        String urlNameString = "http://api.map.baidu.com/" + url;
        String result = "";
        try {
            // 根据地址获取请求
            HttpGet request = new HttpGet(urlNameString);//这里发送get请求
            // 获取当前客户端对象
            HttpClient httpClient = new DefaultHttpClient();
            // 通过请求对象获取响应对象
            HttpResponse response = httpClient.execute(request);

            // 判断网络连接状态码是否正常(0--200都数正常)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
        //....result是用户信息,站内业务以及具体的json转换这里也不写了...
    }
}
